﻿using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Collections.Generic;
using Java.Lang;
using Newtonsoft.Json;

namespace Markdone.Droid
{
	[Activity (Label = "Markdone.Droid", MainLauncher = true, Icon = "@drawable/icon")]
	public class MainActivity : Activity
	{
        private BaseAdapter TaskAdapter;
        private List<TaskItem> Tasks = new List<TaskItem>();
        protected override void OnCreate (Bundle bundle)
		{
			base.OnCreate (bundle);

			// Set our view from the "main" layout resource
			SetContentView (Resource.Layout.Main);

            // Init data
            Tasks.Add(new TaskItem("task0", "this is task0", true));
            Tasks.Add(new TaskItem("task1", "this is task1", false));
            Tasks.Add(new TaskItem("task2", "this is task2", true));
            Tasks.Add(new TaskItem("task3", "this is task0", false));
            Tasks.Add(new TaskItem("task4", "this is task1", false));
            Tasks.Add(new TaskItem("task5", "this is task2", false));
            Tasks.Add(new TaskItem("task6", "this is task0", false));
            Tasks.Add(new TaskItem("task7", "this is task1", false));
            Tasks.Add(new TaskItem("task8", "this is task2", false));
            Tasks.Add(new TaskItem("task9", "this is task0", false));
            Tasks.Add(new TaskItem("task10", "this is task1", false));
            Tasks.Add(new TaskItem("task11", "this is task2", false));
            Tasks.Add(new TaskItem("task12", "this is task0", false));
            Tasks.Add(new TaskItem("task13", "this is task1", false));
            Tasks.Add(new TaskItem("task14", "this is task2", false));
            Tasks.Add(new TaskItem("task15", "this is task0", false));
            Tasks.Add(new TaskItem("task16", "this is task1", false));
            Tasks.Add(new TaskItem("task17", "this is task2", false));
            Tasks.Add(new TaskItem("task18", "this is task0", false));
            Tasks.Add(new TaskItem("task19", "this is task1", false));
            Tasks.Add(new TaskItem("task20", "this is task2", true));

            TaskAdapter = new TaskItemAdapter(this, Tasks);
            // Get our Views from the layout resource,
            // and attach an event to it
            var listView = FindViewById<ListView>(Resource.Id.listViewTask);
            listView.Adapter = TaskAdapter;

            var addButton = new Button(this);
            listView.AddFooterView(addButton);
            addButton.Text = "Add";
            //addButton.Click += delegate
            //{
            //    var task = new TaskItem("task" + Tasks.Count, "this is task" + Tasks.Count, false, false);
            //    task.IsRepeat = true;
            //    task.DateTime = DateTime.Now;
            //    task.Over = DateTime.Now > task.DateTime;
            //    Tasks.Add(task);
                
            //    TaskAdapter.NotifyDataSetChanged();
            //};

            listView.ItemClick += (object sender, AdapterView.ItemClickEventArgs e) =>
            {
                var view = e.View;
                var item = Tasks[e.Position];

                Toast.MakeText(this, "item clicked!", ToastLength.Short).Show();

                var intent = new Intent(this, typeof(TaskDetailActivity));
                var item_string = JsonConvert.SerializeObject((item));
                intent.PutExtra("TASK_POSITION", e.Position);
                intent.PutExtra("TASK_ITEM", item_string);
                this.StartActivityForResult(intent, 1);

            };


        }

        protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);
            
            if(requestCode == 1)
            {
                if(resultCode == Result.Ok)
                {
                    var item = JsonConvert.DeserializeObject<TaskItem>(data.GetStringExtra("result"));
                    var position = data.GetIntExtra("TASK_POSITION", -1);
                    if(position >= 0)
                    {
                        Tasks[position] = item;
                        TaskAdapter.NotifyDataSetChanged();
                    }
                }
                else if(resultCode == Result.Canceled)
                {

                }
            }
        }
    }


}


